package com.sinosoft.app.entrust.dao.imp;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.sinosoft.app.entrust.dao.EntrustAssetsDao;
import com.sinosoft.app.entrust.entity.EntrustAssets;

   /**
    * @ClassName EntrustAssetsDaoJdbc实现类
    * @date 2017-2-20 
    * @author shangxp
    */  
@Repository
public class EntrustAssetsDaoJdbc implements EntrustAssetsDao {
	private static Logger log = Logger.getLogger(EntrustAssetsDaoJdbc.class);
	@Autowired
	private JdbcTemplate jdbcTemplate;

	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	@Override
	public int insertEntrustAssets(EntrustAssets entrustAssets) {
		String sqlStr = "insert into entrust_assets ( "
							+" id ,"	
							+" sys_user_id ,"	
							+" depositor ,"	
							+" assets ,"	
							+" commision ,"	
							+" yield ,"	
							+" entrustdays ,"	
							+" stoploss ,"	
							+" entrustinfo ,"	
							+" entrustate ,"	
							+" releasetime ,"	
							+"is_del) values( "
							 +" ?,"
							 +" ?,"
							 +" ?,"
							 +" ?,"
							 +" ?,"
							 +" ?,"
							 +" ?,"
							 +" ?,"
							 +" ?,"
							 +" ?,"
							 +" ?,"
							 +" 0 )";
		log.debug(sqlStr+"\n"+entrustAssets.toString());
		try{
			Object[] args = {
					entrustAssets.getId(),
					entrustAssets.getSysUserId(),
					entrustAssets.getDepositor(),
					entrustAssets.getAssets(),
					entrustAssets.getCommision(),
					entrustAssets.getYield(),
					entrustAssets.getEntrustdays(),
					entrustAssets.getStoploss(),
					entrustAssets.getEntrustinfo(),
					entrustAssets.getEntrustate(),
					entrustAssets.getReleasetime()
			};
			return jdbcTemplate.update(sqlStr, args);
		}catch(Exception e){
			log.error("数据添加异常 SQL："+sqlStr+"\n"+entrustAssets.toString()+e.getMessage());
		}
		return 0;
	}

	@Override
	public int updateEntrustAssets(EntrustAssets entrustAssets) {
		String sqlStr = "update entrust_assets set "
									+" id = ? , "
									+" sys_user_id = ? , "
									+" depositor = ? , "
									+" assets = ? , "
									+" commision = ? , "
									+" yield = ? , "
									+" entrustdays = ? , "
									+" stoploss = ? , "
									+" entrustinfo = ? , "
									+" entrustate = ? , "
									+" releasetime = ?  "
									+" where id = ?";
		log.debug(sqlStr+"\n"+entrustAssets.toString());
		try{
			Object[] args = {
					entrustAssets.getId(),
					entrustAssets.getSysUserId(),
					entrustAssets.getDepositor(),
					entrustAssets.getAssets(),
					entrustAssets.getCommision(),
					entrustAssets.getYield(),
					entrustAssets.getEntrustdays(),
					entrustAssets.getStoploss(),
					entrustAssets.getEntrustinfo(),
					entrustAssets.getEntrustate(),
					entrustAssets.getReleasetime(),
					entrustAssets.getId()
			};
			return jdbcTemplate.update(sqlStr, args);
		}catch(Exception e){
			log.error("数据更新异常 SQL："+sqlStr+"\n"+entrustAssets.toString()+e.getMessage());
		}
		return 0;
	}

	@Override
	public int deleteEntrustAssets(int id) {
		String sqlStr = "update entrust_assets set is_del = 1  where id = ?";
		try{
			Object[] args = {
					id
			};
			return jdbcTemplate.update(sqlStr, args);
		}catch(Exception e){
			log.error("数据删除异常"+sqlStr+"\nID="+id+"\n"+e.getMessage());
		}	return 0;
	}

	@Override
	public EntrustAssets selectEntrustAssets(int id) {
		String sqlStr = "select * from entrust_assets where is_del =0 and id = ?";
		log.debug(sqlStr+"\nID="+id);
		try{
			Object[] args = {
				id
			};
			return jdbcTemplate.queryForObject(sqlStr, args, new EntrustAssetsMapper());
		}catch(Exception e){
			log.error("数据查询异常"+sqlStr+"\nID="+id+"\n"+e.getMessage());
		}	return null;
	}

	@Override
	public EntrustAssets selectEntrustAssets(List<String> queryKeys,List<Object> queryValues) {
		StringBuffer sqlStr = new StringBuffer("select * from entrust_assets where is_del =0 " );
		Object[] args = null;
		if(queryKeys!=null && !queryKeys.isEmpty()){	
			args = new Object[queryKeys.size()];
			for(int i=0;i<queryKeys.size();i++){
				sqlStr.append(" and "+queryKeys.get(i)+"=? ");
				args[i] = queryValues.get(i);
			}
		}
		log.debug(sqlStr.toString());
		try{
			return jdbcTemplate.queryForObject(sqlStr.toString(), args, new EntrustAssetsMapper());
		}catch(Exception e){
			log.error("数据查询异常"+sqlStr.toString()+"\n"+e.getMessage());
		}
		return null;
	}

	@Override
	public int selectCountEntrustAssets() {
		String sqlStr = "select count(1) from entrust_assets where is_del =0 ";
		log.debug(sqlStr);
		try{
			return jdbcTemplate.queryForObject(sqlStr, Integer.class);
		}catch(Exception e){
			log.error("数据查询异常"+sqlStr+"\n"+e.getMessage());
		}
		return 0;
	}

	@Override
	public List<EntrustAssets> selectLimitEntrustAssets(int start, int size) {
		String sqlStr = "select ";
					sqlStr+=" id ,";
					sqlStr+=" sys_user_id ,";
					sqlStr+=" depositor ,";
					sqlStr+=" assets ,";
					sqlStr+=" commision ,";
					sqlStr+=" yield ,";
					sqlStr+=" entrustdays ,";
					sqlStr+=" stoploss ,";
					sqlStr+=" entrustinfo ,";
					sqlStr+=" entrustate ,";
					sqlStr+=" releasetime ";
			sqlStr+=" from entrust_assets where is_del =0  order by id desc limit ?,?";
		log.debug(sqlStr);
		try{
		Object[] args = {
				start,
				size
		};
			return jdbcTemplate.query(sqlStr, args, new EntrustAssetsMapper());
		}catch(Exception e){
			log.error("数据查询异常"+sqlStr+"\n"+e.getMessage());
		}
		return null;
	}

	@Override
	public List<EntrustAssets> selectAllEntrustAssets() {
		String sqlStr = "select ";
					sqlStr+=" id ,";
					sqlStr+=" sys_user_id ,";
					sqlStr+=" depositor ,";
					sqlStr+=" assets ,";
					sqlStr+=" commision ,";
					sqlStr+=" yield ,";
					sqlStr+=" entrustdays ,";
					sqlStr+=" stoploss ,";
					sqlStr+=" entrustinfo ,";
					sqlStr+=" entrustate ,";
					sqlStr+=" releasetime ";
		 sqlStr+=" from entrust_assets where is_del =0 order by id desc ";
		log.debug(sqlStr);
		try{
			return jdbcTemplate.query(sqlStr, new EntrustAssetsMapper());
		}catch(Exception e){
			log.error("数据查询异常"+sqlStr+"\n"+e.getMessage());
		}
		return null;
	}

	@Override
	public int selectCountEntrustAssets(List<String> queryKeys,List<Object> queryValues) {
		StringBuffer sqlStr = new StringBuffer("select count(1) from entrust_assets where is_del = 0 ");
		Object[] args = null;
		if(queryKeys!=null && !queryKeys.isEmpty()){	
			args = new Object[queryKeys.size()];
			for(int i=0;i<queryKeys.size();i++){
				sqlStr.append(" and "+queryKeys.get(i)+" = ? ");
				args[i] = queryValues.get(i) ;
			}
		}		
		log.debug(sqlStr.toString());
		try{
			return jdbcTemplate.queryForObject(sqlStr.toString(), args,Integer.class);
		}catch(Exception e){
			log.error("数据查询异常"+sqlStr.toString()+"\n"+e.getMessage());
		}
		return 0;
	}

	@Override
	public List<EntrustAssets> selectLimitEntrustAssets(List<String> queryKeys,List<Object> queryValues,int start,int size) {
		StringBuffer sqlStr = new StringBuffer("select ");
					sqlStr.append(" id ,");
					sqlStr.append(" sys_user_id ,");
					sqlStr.append(" depositor ,");
					sqlStr.append(" assets ,");
					sqlStr.append(" commision ,");
					sqlStr.append(" yield ,");
					sqlStr.append(" entrustdays ,");
					sqlStr.append(" stoploss ,");
					sqlStr.append(" entrustinfo ,");
					sqlStr.append(" entrustate ,");
					sqlStr.append(" releasetime ");
			sqlStr.append(" from entrust_assets where is_del =0 " );
		Object[] args = null;
			if(queryKeys!=null && !queryKeys.isEmpty()){	
				args = new Object[queryKeys.size()+2];
				for(int i=0;i<queryKeys.size();i++){
					sqlStr.append(" and "+queryKeys.get(i)+" = ? ");
					args[i] =  queryValues.get(i) ;
				}
			}
		if(args==null){
			args = new Object[]{start,size};
		}else{
			args[queryKeys.size()] = start;
			args[queryKeys.size()+1] = size;
		}
		sqlStr.append(" order by id desc ");
		sqlStr.append(" limit ?,?");
		log.debug(sqlStr.toString());
		try{
			return jdbcTemplate.query(sqlStr.toString(), args, new EntrustAssetsMapper());
		}catch(Exception e){
			log.error("数据查询异常"+sqlStr.toString()+"\n"+e.getMessage());
		}
		return null;
	}

	@Override
	public List<EntrustAssets> selectAllEntrustAssets(List<String> queryKeys,List<Object> queryValues) {
		StringBuffer sqlStr = new StringBuffer("select ");
					sqlStr.append(" id ,");
					sqlStr.append(" sys_user_id ,");
					sqlStr.append(" depositor ,");
					sqlStr.append(" assets ,");
					sqlStr.append(" commision ,");
					sqlStr.append(" yield ,");
					sqlStr.append(" entrustdays ,");
					sqlStr.append(" stoploss ,");
					sqlStr.append(" entrustinfo ,");
					sqlStr.append(" entrustate ,");
					sqlStr.append(" releasetime ");
			sqlStr.append("from entrust_assets where is_del =0 " );
		Object[] args = null;
			if(queryKeys!=null && !queryKeys.isEmpty()){	
				args = new Object[queryKeys.size()];
				for(int i=0;i<queryKeys.size();i++){
					sqlStr.append(" and "+queryKeys.get(i)+" = ? ");
					args[i] = queryValues.get(i);
				}
			}
		sqlStr.append(" order by id desc ");
		log.debug(sqlStr.toString());
		try{
			return jdbcTemplate.query(sqlStr.toString(), args, new EntrustAssetsMapper());
		}catch(Exception e){
			log.error("数据查询异常"+sqlStr.toString()+"\n"+e.getMessage());
		}
		return null;
	}

	protected class EntrustAssetsMapper implements RowMapper<EntrustAssets> {
		@Override
		public EntrustAssets mapRow(ResultSet arg0, int arg1) throws SQLException {
			EntrustAssets entrustAssets = new EntrustAssets();
			if(arg0!=null){
					entrustAssets.setId(arg0.getInt("id"));
					entrustAssets.setSysUserId(arg0.getInt("sys_user_id"));
					entrustAssets.setDepositor(arg0.getString("depositor"));
					entrustAssets.setAssets(arg0.getString("assets"));
					entrustAssets.setCommision(arg0.getString("commision"));
					entrustAssets.setYield(arg0.getString("yield"));
					entrustAssets.setEntrustdays(arg0.getString("entrustdays"));
					entrustAssets.setStoploss(arg0.getString("stoploss"));
					entrustAssets.setEntrustinfo(arg0.getString("entrustinfo"));
					entrustAssets.setEntrustate(arg0.getString("entrustate"));
					entrustAssets.setReleasetime(arg0.getString("releasetime"));
				}
			return entrustAssets;
		}  
	}
}

